package lime.group.view;

import java.util.List;

import lime.common.LimeException;
import lime.group.editor.GroupInput;
import lime.group.editor.GroupPropertyEditor;
import lime.model.Group;
import lime.model.GroupRoot;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.ViewPart;

public class GroupNavigatorView extends ViewPart {

	public static final String ID = "lime.group.view.navigator";

	// Services
	RepositoryService repositoryService;

	// Widgets
	ListViewer groupViewer;

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService");
	}

	@Override
	public void createPartControl(Composite parent) {

		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(2, false));

		final GroupFilter filter = new GroupFilter();
		
		new Label(container, SWT.NONE).setText("Group class:");
		
		Combo groupClassCombo = new Combo(container, SWT.DROP_DOWN | SWT.READ_ONLY);
		List<String> groupClasses = repositoryService.getGroupClasses();
		groupClassCombo.setItems(groupClasses.toArray(new String[] {}));
		groupClassCombo.add("", 0);
		groupClassCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				Combo source = (Combo) event.getSource();
				String groupClass = source.getText();
				filter.setGroupClass(groupClass);
				groupViewer.refresh();
				updateStatusLine();
			}
		});
		
		new Label(container, SWT.NONE).setText("Group name contains:");
		
		Text containsText = new Text(container, SWT.BORDER);
		containsText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		containsText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent event) {
				Text source = (Text) event.getSource();
				String contains = source.getText();
				filter.setContains(contains);
				groupViewer.refresh();
				updateStatusLine();
			}
		});
		
		groupViewer = new ListViewer(container, SWT.BORDER | SWT.V_SCROLL);
		groupViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

		groupViewer.setContentProvider(new BaseWorkbenchContentProvider());
		groupViewer.setLabelProvider(new WorkbenchLabelProvider());
		groupViewer.setInput(new GroupRoot());

		groupViewer.addFilter(filter);
		
		groupViewer.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent event) {
				ISelection selection = event.getSelection();
				if (selection != null && selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					Object element = structuredSelection.getFirstElement();
					if (element instanceof Group) {
						Group group = (Group) element;
						GroupInput groupInput = new GroupInput(group);
						try {
							getSite().getPage().openEditor(groupInput, GroupPropertyEditor.ID);
						} catch (PartInitException e) {
							throw new LimeException(e);
						}
					}
				}
			}
		});


		
		// Add context menu
		MenuManager menuManager = new MenuManager();
		menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		groupViewer.getList().setMenu(menuManager.createContextMenu(groupViewer.getList()));
		getSite().registerContextMenu(menuManager, groupViewer);
		getSite().setSelectionProvider(groupViewer);
	}

	@Override
	public void setFocus() {
		groupViewer.getList().setFocus();
		updateStatusLine();
	}
	
	public void updateStatusLine() {
		int groupCount = groupViewer.getList().getItemCount();
		String message = "Group count: " + groupCount;
		getViewSite().getActionBars().getStatusLineManager().setMessage(message);
	}

}
